<?php
/**
 * $Id:$
 *
 * FGS is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

function sort_players_scores($a, $b) {
	return $a['score'] < $b['score'];
}

class fgsStats {


	function getPlayersScores($auth = true, $limit = 10) {
		$filter = 'PlayerLogin';
		if (!$auth)
			$filter = 'PlayerName';
			
		$query = sprintf('
			SELECT 
				fg.gid,
				ffm.id_auto,
				ffme.player,
				ffme.key, ffme.value
			FROM fgs_fgsd_match ffm
			INNER JOIN fgs_fgsd_games ffg
				ON ffm.id_fgsd_game = ffg.id
			INNER JOIN fgs_games fg
				ON fg.gid = ffg.name
			LEFT JOIN fgs_fgsd_match_entry ffme
				ON ffme.id_match = ffm.id_auto
			WHERE 
				ffm.is_locked = 1
				AND ffme.key IN ("PlayerScore", "%s")
			ORDER BY ffm.id_auto, ffme.key
			',
			$filter
		);
		
		$sql = fgs::sql();
		$sql->query($query);
		$prestats = $sql->fetchAll();
		$prestats2 = $stats = array();
		
		foreach ($prestats as $key => $value)
			$prestats2[$value['gid']][$value['id_auto']][$value['player']][$value['key']] = $value['value'];

		foreach ($prestats2 as $game => $match) {
			foreach ($match as $players) {
				foreach ($players as $player) {
					if (count($player) != 2)
						continue;
					$stats[$game][$player[$filter]]['score'] += $player['PlayerScore'];
					$stats[$game][$player[$filter]]['count'] ++;
				}
			}
			if (is_array($stats[$game]))
				uasort($stats[$game], 'sort_players_scores');
		}
	
		return $stats;
	}

	function getPlayersMatchs($auth = true, $limit = 10) {
		$filter = 'PlayerLogin';
		if (!$auth)
			$filter = 'PlayerName';
		$query = sprintf('
			SELECT  
				COUNT(*) as count,
				ffme.value as name
			FROM fgs_fgsd_match ffm
			INNER JOIN fgs_fgsd_games ffg
				ON ffm.id_fgsd_game = ffg.id
			LEFT JOIN fgs_fgsd_match_entry ffme
				ON ffme.id_match = ffm.id_auto
			WHERE
				ffm.is_locked = 1
				AND ffme.key = "%s"
			GROUP BY ffme.value
			ORDER BY count DESC
			LIMIT %d',
			$filter,
			$limit
		);

		$sql = fgs::sql();
		$sql->query($query);
		$stats = $sql->fetchAll();

		return $stats;
	}

	function getPlayerMatchCount($login, $auth = true) {
		$filter = 'PlayerLogin';
		if (!$auth)
			$filter = 'PlayerName';

		$query = sprintf('
			SELECT  
				COUNT(*) as count
			FROM fgs_fgsd_match ffm
			INNER JOIN fgs_fgsd_games ffg
				ON ffm.id_fgsd_game = ffg.id
			LEFT JOIN fgs_fgsd_match_entry ffme
				ON ffme.id_match = ffm.id_auto
			WHERE
				ffm.is_locked = 1
				AND ffme.key = "%s"
				AND ffme.value = "%s"
			GROUP BY ffme.value',
			$filter,
			$login
		);

		$sql = fgs::sql();
		$sql->query($query);
		$stats = $sql->getline();
		return (int)$stats['count'];
	}

}

?>
